目標:撈值到grid,再讀取打勾的row
add GridView
新增checkbox 轉成template
塞資料
protected void BtnQry_Click(object sender, EventArgs e)
{
System.Data.DataTable dt = new System.Data.DataTable();
dt.Columns.Add("t1");
dt.Columns.Add("t2");
dt.Columns.Add("t3");
dt.Rows.Add();
dt.Rows[0][0] = "test0";
dt.Rows[0][1] = "test1";
dt.Rows[0][2] = "test2";
dt.Rows.Add();
dt.Rows[1][0] = "test10";
dt.Rows[1][1] = "test11";
dt.Rows[1][2] = "test12";
GridView1.DataSource = dt;
GridView1.DataBind();
}
讀讀看
protected void BtnSave_Click(object sender, EventArgs e)
{
string xx = "";
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox cb = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1"); ;//拿check
if (cb.Checked)
{
xx = GridView1.Rows[i].Cells[1].Text;//拿cell裡的值
Response.Write(i.ToString()+xx+"--");
}
}
}
試一下設定chekbox
protected void BtnSelAll_Click(object sender, EventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox cb = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1"); ;
cb.Checked = true;
}
}
protected void BtnCancelSelAll_Click(object sender, EventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox cb = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1"); ;
cb.Checked = false;
}
}